﻿#include "MaterialExpamsionWidget.h"
#include "ui_MaterialExpamsionWidget.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialExpansion.h"

#include <QMessageBox>

namespace GUI
{
    MaterialExpamsionWidget::MaterialExpamsionWidget(EventOper::ParaWidgetInterfaceOperator * oper,Interface::FITKAbaMaterialExpansion * item, QWidget * parent) :
        MaterialWidgetBase(oper,parent), _materialItem(item)
    {
        _ui = new Ui::MaterialExpamsionWidget();
        _ui->setupUi(this);

        initUi();
    }

    MaterialExpamsionWidget::~MaterialExpamsionWidget()
    {
        if (_ui != nullptr)delete _ui;
    }

    bool MaterialExpamsionWidget::checkValue()
    {
        auto checkValue = [=](QString str, int row, int column)->bool {
            //判断数据是否有效
            bool isOk = false;
            str.toDouble(&isOk);
            if (isOk == true)return true;

            QString message = QString(tr("Tabular data for at least one option or suboption\n has either blank or zero valued row(s).\n"));
            message = message + QString(tr("Error: Expansion row: %1\n").arg(row + 1));
            message = message + QString(tr("Error: Expansion column: %1\n").arg(column + 1));
            QMessageBox::warning(this, tr("StructualApp"), message);
            return false;
        };

        int typeIndex = _ui->comboBox_type->currentIndex();
        bool isTemp = _ui->checkBox_temp->isChecked();
        switch (typeIndex) {
        case 0: {
            //强制失去焦点，否则获取数据有错误
            _ui->tableWidget_Isotropic->setEnabled(false);
            _ui->tableWidget_Isotropic->setEnabled(true);
            int rowNum = _ui->tableWidget_Isotropic->rowCount();
            int colNum = _ui->tableWidget_Isotropic->columnCount();
            for (int i = 0; i < rowNum; i++) {
                for (int j = 0; j < colNum; j++) {
                    QString value = "";
                    QTableWidgetItem* item = _ui->tableWidget_Isotropic->item(i, j);
                    if (item == nullptr)continue;
                    value = item->text();

                    if (isTemp == false && j == 1) {
                        continue;
                    }
                    else {
                        if (!checkValue(value, i, j))return false;
                    }
                }
            }
            break;
        }
        case 1: {
            //强制失去焦点，否则获取数据有错误
            _ui->tableWidget_orthotropic->setEnabled(false);
            _ui->tableWidget_orthotropic->setEnabled(true);
            int rowNum = _ui->tableWidget_orthotropic->rowCount();
            int colNum = _ui->tableWidget_orthotropic->columnCount();
            for (int i = 0; i < rowNum; i++) {
                for (int j = 0; j < colNum; j++) {
                    QString value = "";
                    QTableWidgetItem* item = _ui->tableWidget_orthotropic->item(i, j);
                    if (item == nullptr)continue;
                    value = item->text();

                    if (isTemp == false && j == 3) {
                        continue;
                    }
                    else {
                        if (!checkValue(value, i, j))return false;
                    }
                }
            }
            break;
        }
        case 2: {
            //强制失去焦点，否则获取数据有错误
            _ui->tableWidget_Anisotropic->setEnabled(false);
            _ui->tableWidget_Anisotropic->setEnabled(true);
            int rowNum = _ui->tableWidget_Anisotropic->rowCount();
            int colNum = _ui->tableWidget_Anisotropic->columnCount();
            for (int i = 0; i < rowNum; i++) {
                for (int j = 0; j < colNum; j++) {
                    QString value = "";
                    QTableWidgetItem* item = _ui->tableWidget_Anisotropic->item(i, j);
                    if (item == nullptr)continue;
                    value = item->text();

                    if (isTemp == false && j == 6) {
                        continue;
                    }
                    else {
                        if (!checkValue(value, i, j))return false;
                    }
                }
            }
            break;
        }
        }
        return true;
    }
    bool MaterialExpamsionWidget::saveValue()
    {
        if (_materialItem == nullptr)return false;

        //设置材料类型
        Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType type;
        switch (_ui->comboBox_type->currentIndex()) {
        case 0: type = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType::Isotropic; break;
        case 1: type = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType::Orthotropic; break;
        case 2: type = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType::Anisotropic; break;
        }
        _materialItem->setExpansionType(type);

        //设置是否使用温度
        bool tempIsChecked = _ui->checkBox_temp->isChecked();
        _materialItem->setUseTemperatureDependentData(tempIsChecked);

        //存储QTableWidget中的数据
        saveValue(type);

        return true;
    }
    void MaterialExpamsionWidget::on_comboBox_type_activated(int index)
    {
        Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType type;
        switch (index){
        case 0: type = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType::Isotropic; break;
        case 1: type = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType::Orthotropic; break;
        case 2: type = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType::Anisotropic; break;
        }
        changeMaterialElasticType(type);

        //温度参数变化
        on_checkBox_temp_clicked(_ui->checkBox_temp->isChecked());
    }

    void MaterialExpamsionWidget::on_checkBox_temp_clicked(bool checked)
    {
        Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType type;
        switch (_ui->comboBox_type->currentIndex()) {
        case 0: type = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType::Isotropic; break;
        case 1: type = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType::Orthotropic; break;
        case 2: type = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType::Anisotropic; break;
        }
        changeMaterialTemp(type, checked);
    }
    void MaterialExpamsionWidget::initUi()
    {
        if (_materialItem == nullptr)return;
        
        //材料类型添加
        QStringList typeItems = {};
        //数据层暂不支持，后面类型暂注
        typeItems << tr("Isotropic") /*<< tr("Orthotropic") << tr("Anisotropic")*/;
        _ui->comboBox_type->addItems(typeItems);

        //TableWidget初始化
        _ui->tableWidget_Isotropic->addTableRowAndItem(0);
        //设置为单行模式
        _ui->tableWidget_Isotropic->setIsAloneRow(true);

        _ui->tableWidget_orthotropic->addTableRowAndItem(0);
        //设置为单行模式
        _ui->tableWidget_orthotropic->setIsAloneRow(true);

        _ui->tableWidget_Anisotropic->addTableRowAndItem(0);
        //设置为单行模式
        _ui->tableWidget_Anisotropic->setIsAloneRow(true);

        //初始化材料类型
        changeMaterialElasticType(_materialItem->getExpansionType());
        //初始化温度类型
        changeMaterialTemp(_materialItem->getExpansionType(), _materialItem->getUseTemperatureDependentData());

        //设置按钮状态更改
        if (_materialItem->getUseTemperatureDependentData())_ui->checkBox_temp->setCheckState(Qt::Checked);
        else _ui->checkBox_temp->setCheckState(Qt::Unchecked);

        //设置类型选择控件
        auto type = _materialItem->getExpansionType();
        switch (type) {
        case Interface::FITKAbaMaterialBehaviorEnum::Isotropic:_ui->comboBox_type->setCurrentIndex(0); break;
        case Interface::FITKAbaMaterialBehaviorEnum::Orthotropic:_ui->comboBox_type->setCurrentIndex(1); break;
        case Interface::FITKAbaMaterialBehaviorEnum::Anisotropic:_ui->comboBox_type->setCurrentIndex(2); break;
        }

        showValue(_materialItem->getExpansionType());
    }

    void MaterialExpamsionWidget::showValue(Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType type)
    {
        switch (type)
        {
        case Interface::FITKAbaMaterialBehaviorEnum::Isotropic:
        {
            int rowNum = _materialItem->getIsotropicNumberOfRows();
            for (int i = 0; i < rowNum; i++) {
                QTableWidgetItem* item = new QTableWidgetItem(QString::number(_materialItem->getIsotropicExpansionCoeff(i)));
                _ui->tableWidget_Isotropic->setItem(i, 0, item);
                item = new QTableWidgetItem(QString::number(_materialItem->getIsotropicTemperature(i)));
                _ui->tableWidget_Isotropic->setItem(i, 1, item);
            }
            break;
        }
        case Interface::FITKAbaMaterialBehaviorEnum::Orthotropic:
            break;
        case Interface::FITKAbaMaterialBehaviorEnum::Anisotropic:
            break;
        }
    }

    void MaterialExpamsionWidget::saveValue(Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType type)
    {
        if (_materialItem == nullptr)return;
        bool isTemp = _ui->checkBox_temp->isChecked();
        switch (type){
        case Interface::FITKAbaMaterialBehaviorEnum::Isotropic:
        {
            //设置材料参数行数
            int rowNum = _ui->tableWidget_Isotropic->rowCount();
            _materialItem->setIsotropicNumberOfRows(rowNum);
            for (int i = 0; i < rowNum; i++) {
                QTableWidgetItem* item = nullptr;
                double value = 0;

                item = _ui->tableWidget_Isotropic->item(i, 0);
                if (!item)value = 0;
                else value = item->text().toDouble();
                _materialItem->setIsoTropicExpansionCoeff(value, i);

                //判断温度是否使用
                if (isTemp) {
                    item = _ui->tableWidget_Isotropic->item(i, 1);
                    if (!item)value = 0;
                    else value = item->text().toDouble();
                }
                else value = 0;
                _materialItem->setIsotropicTemperature(value, i);
            }
            break;
        }
        case Interface::FITKAbaMaterialBehaviorEnum::Orthotropic:
            break;
        case Interface::FITKAbaMaterialBehaviorEnum::Anisotropic:
            break;
        }
    }

    void MaterialExpamsionWidget::changeMaterialElasticType(Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType type)
    {
        //隐藏所有的TableWidget
        _ui->groupBox_Isotropic->hide();
        _ui->groupBox_orthotropic->hide();
        _ui->groupBox_Anisotropic->hide();

        //根据类型显示对应的QTableWidget
        switch (type){
        case Interface::FITKAbaMaterialBehaviorEnum::Isotropic:_ui->groupBox_Isotropic->show(); break;
        case Interface::FITKAbaMaterialBehaviorEnum::Orthotropic:_ui->groupBox_orthotropic->show(); break;
        case Interface::FITKAbaMaterialBehaviorEnum::Anisotropic:_ui->groupBox_Anisotropic->show(); break;
        }
    }
    
    void MaterialExpamsionWidget::changeMaterialTemp(Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialExpansionType type, bool isTemp)
    {
        //隐藏所有温度参数
        _ui->tableWidget_Isotropic->hideColumn(1);
        _ui->tableWidget_orthotropic->hideColumn(3);
        _ui->tableWidget_Anisotropic->hideColumn(6);
   
        //type对应的QTableWidget温度参数开启
        if (isTemp == false)return;
        switch (type) {
        case Interface::FITKAbaMaterialBehaviorEnum::Isotropic:_ui->tableWidget_Isotropic->showColumn(1); break;
        case Interface::FITKAbaMaterialBehaviorEnum::Orthotropic:_ui->tableWidget_orthotropic->showColumn(3); break;
        case Interface::FITKAbaMaterialBehaviorEnum::Anisotropic:_ui->tableWidget_Anisotropic->showColumn(6); break;
        }
    }
}
